package com.example.boxuegu;

import androidx.appcompat.app.AppCompatActivity;

import android.content.Context;
import android.content.SharedPreferences;
import android.content.pm.ActivityInfo;
import android.os.Bundle;
import android.text.TextUtils;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.Toast;

public class find_psw extends AppCompatActivity {

    private String from;
    private TextView tv_main_title;
    private TextView tv_back;
    private EditText et_validate_name;
    private TextView tv_reset_psw;
    private Button but_validate;
    private TextView tv_usesr_name;
    private EditText et_user_name;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_find_psw);
        setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_PORTRAIT);//设置为竖屏
        from = getIntent().getStringExtra("from");
        init();
    }

    private void init() {
        tv_main_title = (TextView) findViewById(R.id.tv_main_title);
        tv_back = (TextView) findViewById(R.id.tv_back);
        et_validate_name = (EditText) findViewById(R.id.et_validate_name);
        tv_reset_psw = (TextView) findViewById(R.id.tv_reset_psw);
        but_validate = (Button) findViewById(R.id.but_validate);//验证按钮
        tv_usesr_name = (TextView) findViewById(R.id.tv_user_name);
        et_user_name = (EditText) findViewById(R.id.et_user_name);
        if("security".equals(from)){
            tv_main_title.setText("设置密保");
        }else{
            tv_main_title.setText("找回密码");
            tv_usesr_name.setVisibility(View.VISIBLE);
            et_user_name.setVisibility(View.VISIBLE);
        }
        tv_back.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                find_psw.this.finish();
            }
        });
        but_validate.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                String validName = et_validate_name.getText().toString().trim();
                if("security".equals(from)){
                    if(TextUtils.isEmpty(validName)){
                        Toast.makeText(find_psw.this,"请输入验证的姓名",Toast.LENGTH_SHORT).show();
                        return;
                    }else{
                        Toast.makeText(find_psw.this,"密保设置成功",Toast.LENGTH_SHORT).show();
                        saveSecurity(validName);
                        find_psw.this.finish();
                    }
                }else{
                    String userName =et_validate_name.getText().toString().trim();
                    String sp_security=readSecurity(userName);
                    if(TextUtils.isEmpty(userName)){
                        Toast.makeText(find_psw.this,"请输入您的用户名",Toast.LENGTH_SHORT).show();
                        return;
                    }else if(!isExistUserName(userName)){
                        Toast.makeText(find_psw.this,"您输入的用户名不存在",Toast.LENGTH_SHORT).show();
                        return;
                    }else if(TextUtils.isEmpty(validName)){
                        Toast.makeText(find_psw.this,"请输入要验证得姓名",Toast.LENGTH_SHORT).show();
                        return;
                    }if(!validName.equals(sp_security)){
                        Toast.makeText(find_psw.this,"输入密保不正确",Toast.LENGTH_SHORT).show();
                        return;
                    }else{
                        //输入密保正确，重新给用户设置密码
                        tv_reset_psw.setVisibility(View.VISIBLE);
                        tv_reset_psw.setText("初始密码：123456");
                        savePsw(userName);
                    }

                }

            }
        });
    }

    private void savePsw(String userName) {
        String md5=MD5.md5("123456");//把密码用md5加密
        SharedPreferences loginInfo = getSharedPreferences("loginInfo", MODE_PRIVATE);
        SharedPreferences.Editor edit = loginInfo.edit();
        edit.putString(userName,md5);
        edit.commit();
    }
    /*
    * 从sharedPreferences中根据用户名来判断是否有此用户*/

    private boolean isExistUserName(String userName) {
        boolean hasUserName=false;
        SharedPreferences loginInfo = getSharedPreferences("loginInfo", MODE_PRIVATE);
        String spPsw = loginInfo.getString(userName, "");
        if(!TextUtils.isEmpty(spPsw)){
            hasUserName=true;
        }
        return hasUserName;
    }
    /*
    * 读取密保*/

    private String readSecurity(String userName) {
        SharedPreferences loginInfo = getSharedPreferences("loginInfo", Context.MODE_PRIVATE);
        String security = loginInfo.getString(userName + "_security", "");
        return security;
    }

    /*
    * 保存密码到sharedPreferences中*/
    private void saveSecurity(String validName) {
        SharedPreferences loginInfo = getSharedPreferences("loginInfo", MODE_PRIVATE);
        SharedPreferences.Editor edit = loginInfo.edit();//获取编辑器
        edit.putString(AnalysisUtils.readLoginUserName(this)+"_security",validName);//存入用户对应得密保
        edit.commit();//提交修改
    }
}